package Server.Model.Hall;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import common.routine.RedisUtil;
import net.sf.json.JSONObject;
import rank.RankRichDef;
import redis.clients.jedis.Jedis;
import redis.protocal.DbInfoMgr;

public class SimpleRankUtil {
	private static SimpleRankUtil _instance = null;
	public static SimpleRankUtil getInstance(){
		if(_instance == null){
			_instance = new SimpleRankUtil();
			_instance.init();
		}
		return _instance;
	}
	public void init(){
	}
	public JSONObject[] quickSortTop100(Map<String,String> alls,String intkey,int limit){
		JSONObject [] jsonall= new JSONObject[alls.size()];
		int index = 0;
		for(String data : alls.values()){
			JSONObject obj = JSONObject.fromObject(data);
			if(!obj.containsKey(intkey)){
				continue;
			}
			int value = obj.optInt(intkey);
			if(value < limit){
				continue;
			}
			jsonall[index++] = obj;
		}
		if(index <= 1){
			return jsonall;
		}
		quickSortJSONType(jsonall,intkey,0,index - 1);
		return jsonall;
	}
	public void quickSortJSONType(JSONObject[] data, String intkey,int low, int high) {
		if(data.length == 0)
			return;
		int start = low;
		int end = high;
		JSONObject key = data[low];
		int keyValue = key.optInt(intkey);

		while (end > start) {
			// 从后往前比较
			while (end > start && data[end].optInt(intkey) >= keyValue)
				end--;
			if (data[end].optInt(intkey) <= keyValue) {
				JSONObject temp = data[end];
				data[end] = data[start];
				data[start] = temp;
			}
			// 从前往后比较
			while (end > start && data[start].optInt(intkey) <= keyValue)
				start++;
			if (data[start].optInt(intkey) >= keyValue) {
				JSONObject temp = data[start];
				data[start] = data[end];
				data[end] = temp;
			}
		}
		// 递归
		if (start > low)
			quickSortJSONType(data, intkey,low, start - 1);
		if (end < high)
			quickSortJSONType(data, intkey,end + 1, high);
	}
}
